﻿using Studio77.GuessTheWordsWin8.Entities;
using Studio77.GuessTheWordsWin8.Models;
using Studio77.GuessTheWordsWin8.Utils;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage;

namespace Studio77.GuessTheWordsWin8.Core
{
    public class StageManager
    {
        public static StageManager Instance = new StageManager();

        public static bool IsLoaded = false;

        public List<StageItemModel> stageModels;

        /// <summary>
        /// 关卡数
        /// </summary>
        public const int StageCount = 181;

        private StageManager()
        {
            stageModels = new List<StageItemModel>();
        }


        //keys
        private const string STAGE_DATA_KEY = "stageDataKey";
        private const string LAST_STAGE_KEY = "lastStageKey";
        private const string LAST_SCORE = "lastScore";
        private const string LAMP_COUNT = "lampCount";
        private const string MARKET_COMMENT = "marketComment";
        private const string LAUNCH_COUNT = "launchCount";
        private const string NEW_USER_TASK_KEY = "newUserTaskKey";
        private const string IS_SHOWED_GUIDE_KEY = "isShowedGuideKey";

        private int lastStage;
        public int LastStage
        {
            get
            {
                return lastStage;
            }
            set
            {
                lastStage = value;
                LocalDataUtil.SaveData(LAST_STAGE_KEY, lastStage);
            }
        }

        private int lastScore = 0;
        public int LastScore
        {
            get
            {
                return lastScore;
            }
            set
            {
                lastScore = value;
                LocalDataUtil.SaveData(LAST_SCORE, lastScore);
            }
        }

        private bool isDoNewUserTask = false;
        public bool IsDoNewUserTask
        {
            get
            {
                return isDoNewUserTask;
            }
            set
            {
                isDoNewUserTask = value;
                LocalDataUtil.SaveData(NEW_USER_TASK_KEY, isDoNewUserTask);
            }
        }

        private bool isShowedGuide = false;
        public bool IsShowedGuide
        {
            get
            {
                return isShowedGuide;
            }
            set
            {
                isShowedGuide = value;
                LocalDataUtil.SaveData(IS_SHOWED_GUIDE_KEY, isShowedGuide);
            }
        }

        private int lampCount = 0;
        public int LampCount
        {
            get
            {
                return lampCount;
            }
            set
            {
                lampCount = value;
                LocalDataUtil.SaveData(LAMP_COUNT, lampCount);
            }
        }

        private bool isCommented = false;
        public bool IsCommented
        {
            get
            {
                return isCommented;
            }
            set
            {
                isCommented = value;
                LocalDataUtil.SaveData(MARKET_COMMENT, isCommented);
            }
        }

        private int launchCount = 0;
        public int LaunchCount
        {
            get
            {
                return launchCount;
            }
            set
            {
                launchCount = value;
                LocalDataUtil.SaveData(LAUNCH_COUNT, launchCount);
            }
        }

        public Dictionary<int, StageData> stageDatas;
        public Dictionary<int, MapInfo> mapInfos;

        public async void saveStageDatas()
        {
            await LocalDataUtil.XMLSerialize(stageDatas, typeof(Dictionary<int, StageData>));
        }

        /// <summary>
        /// 载入所有关卡
        /// </summary>
        public async void InitAllStages()
        {
            mapInfos = new Dictionary<int, MapInfo>(StageCount);

            // 读取关卡信息
            for (int i = 1; i <= StageCount; i++)
            {
                MapInfo info = await WordMapLoader.LoadMap(i);
                mapInfos.Add(i, info);
            }

            if (LocalDataUtil.IsSetData(LAST_STAGE_KEY))
            {
                lastStage = (int)LocalDataUtil.GetData(LAST_STAGE_KEY);
            }

            if (LocalDataUtil.IsSetData(LAST_SCORE))
            {
                lastScore = (int)LocalDataUtil.GetData(LAST_SCORE);
            }

            if (LocalDataUtil.IsSetData(LAMP_COUNT))
            {
                lampCount = (int)LocalDataUtil.GetData(LAMP_COUNT);
            }
            else
            {
                LampCount = 10;
            }

            if (LocalDataUtil.IsSetData(NEW_USER_TASK_KEY))
            {
                isDoNewUserTask = (bool)LocalDataUtil.GetData(NEW_USER_TASK_KEY);
            }

            if (LocalDataUtil.IsSetData(MARKET_COMMENT))
            {
                isCommented = (bool)LocalDataUtil.GetData(MARKET_COMMENT);
            }

            if (LocalDataUtil.IsSetData(LAUNCH_COUNT))
            {
                launchCount = (int)LocalDataUtil.GetData(LAUNCH_COUNT);
            }

            if (LocalDataUtil.IsSetData(IS_SHOWED_GUIDE_KEY))
            {
                isShowedGuide = (bool)LocalDataUtil.GetData(IS_SHOWED_GUIDE_KEY);
            }

            if (await LocalDataUtil.ExistFile())
            {
                var obj = await LocalDataUtil.XMLDeserialize(typeof(Dictionary<int, StageData>));
                stageDatas = (Dictionary<int, StageData>)obj;
            }
            else
            {
                stageDatas = new Dictionary<int, StageData>();
                stageDatas.Add(1, new StageData());
                saveStageDatas();
            }

            IsLoaded = true;
            if (MainPage.Instance != null)
            {
                MainPage.Instance.HideProgress();
            }

            for (int i = 1; i <= StageCount; i++)
            {
                StageItemModel model = new StageItemModel();
                if (stageDatas.ContainsKey(i))
                {
                    StageData sd = stageDatas[i];
                    model.CompleteCount = sd.ComplateCount + "/" + mapInfos[1].wordsCount;
                    model.IsLocked = false;
                }
                else
                {
                    model.CompleteCount = "0/" + mapInfos[i].wordsCount;
                    model.IsLocked = true;
                }
                model.StageIndex = i;

                stageModels.Add(model);
            }
        }

        internal StageData getCurrentStageData()
        {
            return stageDatas[LastStage];
        }

        internal void ClearCurrentStageDate()
        {
            stageDatas[LastStage] = new StageData();
        }

        internal void UpdateStageModels()
        {
            foreach (var model in stageModels)
            {
                if (stageDatas.ContainsKey(model.StageIndex))
                {
                    StageData sd = stageDatas[model.StageIndex];
                    model.CompleteCount = sd.ComplateCount + "/" + StageManager.Instance.mapInfos[model.StageIndex].wordsCount;
                    model.IsLocked = false;
                }
            }
        }
    }
}
